<?

/*	Menu TEMPLATE (meta sensitive)
*	
*	renders a menu extended meta values.
*
*	Paramaters: none
*	
*
*					*/

global $Engine;


if( $_set = engine_get("style:menu")) {
	$_set = json_decode( $_set );
	$graphic = ( $_set && $_set->type == "graphic" );
			
	function parse_graphic( $act, $name ) { return "<img src='/text.img?tpe=menu" . ( $act? ".a" : "") . "&str=" . htmlentities( urlencode( $name )) . "' alt='$name' />"; }
}


$map = $Engine->map->filter();

foreach( $map as $node ) {
	global $Page;
		
	$active = ( $node['name'] == PAGE || $node['ID'] == $Page->get( 'parent' ));
	
	if( $_set->upper ) $node['title'] = strtoupper( $node['title'] );
	$content = $graphic?
		parse_graphic( $active, $node['title']) : $node['title'];
		
	$menu .= "<li id='menu.". $node['name'] ."' ". ( $active? "class='active'":"" ) .">";
	$menu .= meta_options( $node, $content ) . "</li>\n";

}


function meta_options( $node, $content ) {
	global $map;
	
	$Pge = new PAGE( $id = (int) $node['id'] );
				
	if( $meta = $Pge->get( 'meta' ))
		if ( $meta->noclick ) {
    		$return = "<a rel='$id'>";
					
		} else if ( $meta->todesc ) {
   			
			$return = "<a href='/" . $node['name'] . "/" . $node['subs'][ 0 ]['name'] . "' rel='$id' >";
					
		} else if ( $meta->alias ) {
   			if( $parent = (int) $_SESSION['map']['num'][$meta->alias]['parent'] ) $parent = "/" . $_SESSION['map'][$parent]['name'];
			$return = "<a href='$parent/" . $_SESSION['map']['num'][$meta->alias]['name'] . "' rel='$id'>";
					
		}
				
	return ( ( !$return )? "<a href='/". $node['name'] ."' rel='$id'>" : $return ) . "$content</a>";
}


/*if( $_set = engine_get("style:menu")) {
	$_set = json_decode($_set);
	$graphic = ( $_set && $_set->type == "graphic" );
			
	function parse_graphic( $act, $name ) { return "<img src='/text.img?tpe=menu" . ( $act? ".a" : "") . "&str=" . htmlentities( urlencode( $name )) . "' alt='$name' />"; }
}
		
function meta_options( $node, $content ) {
				
	if( $meta = json_decode( $node['meta'] ))
		if ( $meta->noclick ) {
    		$return = "<a rel='{$node['ID']}'>";
					
		} else if ( $meta->todesc ) {
   			$subs = explode( ",", $node['sub'] );
			$return = "<a href='/" . $node['name'] . "/" . $_SESSION['map']['num'][$subs[0]]['name'] . "' rel='{$node['ID']}' >";
					
		} else if ( $meta->alias ) {
   			if( $parent = (int) $_SESSION['map'][$meta->alias]['parent'] ) $parent = "/" . $_SESSION['map'][$parent]['name'];
			$return = "<a href='$parent/" . $_SESSION['map'][$meta->alias]['name'] . "' rel='{$node['ID']}'>";
					
		}
				
	return ( ( !$return )? "<a href='/". $node['name'] ."' rel='{$node['ID']}'>" : $return ) . "$content</a>";
}

		
foreach( $_SESSION['map']['menu'] as $node ) {
	global $Page;
	$active = ($node['name'] == PAGE || $node['ID'] == $Page->get('parent') );
	if( $node['active'] == 1 || $active) {
		
		if( $_set->upper ) $node['title'] = strtoupper( $node['title'] );
		$content = $graphic? parse_graphic( $active, $node['title']) : $node['title'];
		$menu .= "<li id='menu.". $node['name'] ."' ". ( $active? "class='active'":"" ) .">" . meta_options( $node, $content ) . "</li>\n";
	}
}	*/	
?>

<ul id='menu'><?= $menu ?></ul>